package com.consumer.demo2.controller;


import com.api.demo.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.PrintWriter;

@RestController
@RequestMapping(value = "/consumer")
public class DeptController_consumer {


//    private static final String PREFIX_URL = "http://127.0.0.1:8011/";

    /**
     * 通过为服务名在eureka上找到微服务
     */
    private static final String PREFIX_URL = "http://MICROSERVICECLOUD-DEPT/";

    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping(value = "/test")
    public String  test(){
        return "test";
    }

    @RequestMapping(value = "/dept/add")
    public void add(Dept dept){
        System.out.println(restTemplate.postForObject(PREFIX_URL + "dept/add" ,dept, String.class));
    }


    @RequestMapping(value = "/dept/get/{id}")
    public void get(@PathVariable Long id, PrintWriter out){
        System.out.println(restTemplate.getForObject(PREFIX_URL + "dept/get/" + id, String.class));
    }


    @RequestMapping(value = "/dept/list")
    public void list(){
        System.out.println(restTemplate.getForObject(PREFIX_URL + "dept/list", String.class));
    }

    /**
     * 消费端调用服务发现
     */
    @RequestMapping(value = "/dept/discovery")
    public void discovery(){
        System.out.println(restTemplate.getForObject(PREFIX_URL + "dept/discovery", String.class));
    }


}
